package com.android.projectsquirrel;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ProfileListing extends ListActivity{
	
	
	List<String> profileItems = new ArrayList<String>();
	 public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
	        setContentView(R.layout.profile_listing);
	        ListView lv = getListView();
	  	  	lv.setTextFilterEnabled(true);     
	  	  	Resources res = getResources();
	  	  	profileItems = getProfileItems();
	  	 	setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, profileItems));
	  	 	lv.setOnItemClickListener(new OnItemClickListener() {
	  	  	    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
	  	  	    	String selectedActivity = ((TextView) view).getText().toString();
		  	  	    Intent myIntent = new Intent(view.getContext(), ProfileDetail.class);
	            	myIntent.putExtra("profilename",selectedActivity);
	                startActivity(myIntent);
	                
	                
	  	  	 }
	    	  });
	  	 	
	  	 	Button squirrelButton = (Button) findViewById(R.id.home);
	     	squirrelButton.setOnClickListener(new View.OnClickListener() {
	             public void onClick(View view) {
	             	Intent myIntent = new Intent(view.getContext(),Main.class);
	                 startActivity(myIntent);
	             }
	     	});
	  	 	
	  	 	
	 }
	 
	 public void OnResume(){
			super.onResume();
			DBAdapter db = new DBAdapter(this);
			db.open();
		}
		
		public void onPause(){
			super.onPause();
			DBAdapter db = new DBAdapter(this);
			db.close();
		}
		
//		getFields()
		public void show(String str){
			Toast.makeText(this,str,Toast.LENGTH_LONG);
		}
		public List<String> getProfileItems(){
	    	Context context = getApplicationContext();
	    	List<String> profiles = new ArrayList<String>();
	    	DBAdapter db = new DBAdapter(this);
	    	db.open();
	    	Cursor c = db.getAllTitles();
	    	startManagingCursor(c);
	    	if(c.moveToFirst()){
	    		if(c.getColumnCount()!=0){
	    			 while (c.isAfterLast() == false) {
	    				 profiles.add(c.getString(0));
	    		       	  c.moveToNext();
	    		        }
	    		}

	    	else {
	    		Toast toast = Toast.makeText(context, "Sorry. There are no saved profiles", Toast.LENGTH_LONG);
				toast.show();	
	    	}
	    	c.close();
	    	db.close();
	    	
	    }
	    	return profiles;
	}
}

